﻿namespace net.zxteam.ZXLexer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.IO;

    internal class ParserFactory
    {
        private readonly static ParserFactory _instance = new ParserFactory();
        private readonly Dictionary<string, Type> _parserTypes = new Dictionary<string, Type>();
        private readonly object _syncRoot = new object();

        public static ParserFactory Instance { get { return _instance; } }
        private ParserFactory()
        {
            lock (_syncRoot)
            {
                AppDomain.CurrentDomain.AssemblyLoad += AssemblyLoad;
				//foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
				//{
				//	ChechAndRegisterParserType(a);
				//}
            }
        }

        public void Ping() { }

        private void AssemblyLoad(object sender, AssemblyLoadEventArgs args)
        {
            lock (_syncRoot)
            {
//                ChechAndRegisterParserType(args.LoadedAssembly);
            }
        }

		//private bool ChechAndRegisterParserType(Assembly potentialParserAssembly)
		//{
		//	bool foundParserFlag = false;

		//	foreach (Type potentialParserType in potentialParserAssembly.GetTypes())
		//	{
		//		if (potentialParserType != null && typeof(IParser).IsAssignableFrom(potentialParserType))
		//		{
		//			object[] attrs = potentialParserType.GetCustomAttributes(typeof(ParserInfoAttribute), false);
		//			if (attrs != null && attrs.Length == 1 && attrs[0] is ParserInfoAttribute)
		//			{
		//				ParserInfoAttribute attr = (ParserInfoAttribute)attrs[0];
		//				_parserTypes.Add(attr.AnnotationNamespace, potentialParserType);
		//				foundParserFlag = true;
		//			}
		//		}
		//	}

		//	return foundParserFlag;
		//}

		//public IParser CreateParser(string annotationNamespace)
		//{
		//	if (!_parserTypes.ContainsKey(annotationNamespace))
		//	{
		//		throw new InvalidOperationException("Parser was not found for a '" + annotationNamespace + "' annotation namespace.");
		//	}

		//	Type parserType = _parserTypes[annotationNamespace];
		//	return (IParser)Activator.CreateInstance(parserType);
		//}
    }
}
